package com.sanyang.lawyer.controller.Log;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.sanyang.lawyer.entity.Case_law;
import com.sanyang.lawyer.entity.Case_type;
import com.sanyang.lawyer.entity.Log_Stair;
import com.sanyang.lawyer.entity.Log_Table;
import com.sanyang.lawyer.mapper.Log.LogMapper;
import com.sanyang.lawyer.service.Case.Case_lawService;
import com.sanyang.lawyer.service.Case.Case_typeService;
import com.sanyang.lawyer.service.Log.LogService;
import com.sanyang.lawyer.service.Log.Log_Stair_Service;
import org.apache.ibatis.annotations.Case;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @program: lawyer
 * @description: 日志的Controller层
 * @author: Mr.Wang
 * @create: 2019-10-11 09:16
 **/

@RestController
@RequestMapping("log_table")
public class LogController {

    @Autowired
    private LogService logservice;
    @Autowired
    private Log_Stair_Service log_stair_service;
    @Autowired
    private Case_typeService case_typeService;
    @Autowired
    private Case_lawService case_lawService;


    //返回log表的全部
    @RequestMapping("listall")
    public  List<Log_Table> listall(int userId){
        List<Log_Table> list=logservice.listall(userId);
        return list;
    }

    //返回log表的全部
    @RequestMapping("listall1")
    public  List<Log_Table> listall1(){
        List<Log_Table> list=logservice.listall1();
        return list;
    }

    //删除
    @RequestMapping("delete")
    public String  delete(int logId){
        System.out.println(logId);
        logservice.delete(logId);
        return "1";
    }


    //根据id查询log表
    public Log_Table getbyid(int log_id){
        Log_Table log=logservice.getbyid(log_id);
        return log;
    }

    //案件类型查询全部
    @RequestMapping("caseType")
    public  List<Case_type> typelistall(){
        List<Case_type> typelist=case_typeService.queryParent();
        return typelist;
    }

    //模糊查询
    @RequestMapping("fuzzyquery")
    public List<Log_Table> fuzzyquery(String state,String kdate,String jdate,String typeID,String casetype,String input,String inputtype,String userId){
        Map<String,String> map=new HashMap<String,String>();
        if(state!=null&&state!=""&&state!="-1"){
            map.put("state",state);
        }
        if(kdate!=null&&kdate!=""){
            map.put("kdate",kdate);
        }
        if(jdate!=null&&jdate!=""){
            map.put("jdate",jdate);
        }
        if(typeID!=null&&typeID!=""&&!typeID.equals("-1")){
            if(!typeID.substring(4,6).equals("00")){
               map.put("typeID",typeID);
            }else {
                String typeid=null;
                typeid=typeID;
                map.put("typeid",typeid);
            }
        }
        if(userId!=null&&userId!=""){
            map.put("userId",userId);
        }
        if(casetype!=null&&casetype!=""&&!casetype.equals("-1")){
            map.put("casetype",casetype);
        }
        switch (inputtype){
            case "1":
                String caseid=null;
                caseid=input;
                if(caseid!=null&&caseid!=""&&!caseid.equals("")){
                    map.put("caseid",caseid);
                }
                break;
            case "2":
                String clientname=null;
                clientname=input;
                if(clientname!=null&&clientname!=""&&!clientname.equals("")){
                    map.put("clientname",clientname);
                }
                break;
            case "3":
                String empname=null;
                empname=input;
                if(empname!=null&&empname!=""&&!empname.equals("")){
                    map.put("empname",empname);
                }
                break;
        }
        List<Log_Table> list=logservice.fuzzyquery(map);
        return list;
    }

    //查看  查询全部
    @RequestMapping("examine")
    public Log_Table examine(int id){
        Log_Table logexamine=logservice.getbyid(id);
        return logexamine;
    }


    //日志类型查询全部
    @RequestMapping("logtype")
    public  List<Log_Stair> logType() {
        List<Log_Stair> list=log_stair_service.listall();
        return list;
    }

    //日志类型查询一级类型。。。。
    @RequestMapping("postlog")
    public List<Log_Stair> postlog(){
        List<Log_Stair> list=log_stair_service.listlog();
        return list;
    }

    @RequestMapping("postlogej")
    public List<Log_Stair> postlogej(int id){
        Log_Table logexamine=logservice.getbyid(id);
        List<Log_Stair> list=log_stair_service.li1(logexamine.getTypeId().getTypeId());
        return list;
    }
        //根据id查询全部二级菜单
    @RequestMapping("postlogejld")
    public List<Log_Stair> postlogejld(int id){
        List<Log_Stair> list=log_stair_service.li1(id);
        return list;
    }

    @RequestMapping("datetime")
    public String datetime(String starttime,String enttime) throws ParseException {
        String DATE_FORMAT_YMDHMS_WITH_T="yyyy-MM-dd'T'HH:mm";
        SimpleDateFormat format = new SimpleDateFormat(DATE_FORMAT_YMDHMS_WITH_T);
        Date date1 = format.parse(starttime);
        Date date2 = format.parse(enttime);
        //计算差值，分钟数
        long minutes = (date2.getTime() - date1.getTime()) / (1000 * 60);

        System.out.println("minutes=="+minutes);
        return minutes+"";
    }



    //查询案件所有
    @RequestMapping("caselist")
    public List<Case_law> caselist(){
        List<Case_law> list=case_lawService.listall();
        return list;
    }
    @RequestMapping("submit")
    public String  submit(String caseId){
        Case_law ca=case_lawService.queryByid(caseId);
        String cname=ca.getClientId().getClientName();
        return cname;
    }

    @RequestMapping("timesj")
    public String  timesj(@RequestParam("data")String data) throws Exception{
        Log_Table logtable=new Log_Table();
        ObjectMapper objectMapper = new ObjectMapper();
        Log_Table logTable=objectMapper.readValue(data,logtable.getClass());
        logservice.update(logTable);
        return "1";
    }

    @RequestMapping("insert")
    public String insert(@RequestParam("data")String data) throws IOException {
        Log_Table logtable=new Log_Table();
        ObjectMapper objectMapper = new ObjectMapper();
        Log_Table logTable=objectMapper.readValue(data,logtable.getClass());
        logservice.insert(logTable);
        return "1";
    }

    @RequestMapping("casequery")
    public List<Case_law> casequery(String casetext,String  casequery){
        Map<String,String> map=new HashMap<String, String>();
        if(casetext!=""&&casetext!=null){
            switch (casequery){
                case "1":
                    String casetext1=null;
                    casetext1=casetext;
                    map.put("casetext1",casetext1);  //案号
                    break;
                case "2":
                    String casetext2=null;
                    casetext2=casetext;
                    map.put("casetext2",casetext2);  //承办律师
                    break;
                case "3":
                    String casetext3=null;
                    casetext3=casetext;
                    map.put("casetext3",casetext3);  //委托人
                    break;
                case "4":
                    String casetext4=null;
                    casetext4=casetext;
                    map.put("casetext4",casetext4);  //当事人
                    break;
                case "5":
                    String casetext5=null;
                    casetext5=casetext;
                    map.put("casetext5",casetext5);  //案由
                    break;
                case "6":
                    String casetext6=null;
                    casetext6=casetext;
                    map.put("casetext6",casetext6);  //受理法院
                    break;
            }
        }
        List<Case_law> list=case_lawService.casequery(map);
        return list;
    }
    
}
